#!/usr/bin/env python
# -*- coding: latin-1; py-indent-offset:4 -*-
################################################################################
# 
# This file is part of Bfplusplus
#
# Bfplusplus is a graphical interface to the Betfair Betting Exchange
# Copyright (C) 2010 Daniel Rodriguez (aka Daniel Rodriksson)
# Copyright (C) 2011 Sensible Odds Ltd.
#
# You can learn more and contact the author at:
#
#    http://code.google.com/p/bfplusplus/
#
# Bfplusplus is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Bfplusplus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Bfplusplus. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""Subclass of TransientMessage, which is generated by wxFormBuilder."""

import wx

import MainGui

# Implementing TransientMessage
class TransientMessage(MainGui.TransientMessage):
    fullTrans = 0
    fullOpaque = 255

    def __init__(self, parent, xrel=50, yrel=50, timeout=5.0, timerInc=0.1):
	MainGui.TransientMessage.__init__(self, parent)

        self.xrel = xrel / 100.0
        self.yrel = yrel / 100.0

        self.timeout = timeout
	self.timerInc = timerInc
        self.timerCount = 0
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)

	self.transInc = int(float(self.fullOpaque) / (self.timeout / self.timerInc))
	self.transLevel = self.fullOpaque

	self.entered = 0

        self.focusFromShow = False


    def Show(self, toShow):
        isShown = self.IsShown()
	if not toShow:
	    self.timer.Stop()

	else:
	    if not self.IsShown():
		self.CalcAndSetPosition()

	    self.timerCount = 0
	    if not self.timer.IsRunning():
		self.timer.Start(self.timerInc * 1000)
	    self.transLevel = self.fullOpaque
	    if self.CanSetTransparent():
		self.SetTransparent(self.transLevel)

        if toShow != isShown:
            if toShow:
                self.focusFromShow = True
            MainGui.TransientMessage.Show(self, toShow)

	self.Refresh()


    def CalcAndSetPosition(self):
	parentSize = self.GetParent().GetSize()
	mySize = self.GetSize()

        maxXCoord = parentSize.width - mySize.width
        xCoord = maxXCoord * self.xrel

        maxYCoord = parentSize.height - mySize.height
        yCoord = maxYCoord * self.yrel

        position = wx.Point(xCoord, yCoord)
        position = self.GetParent().ClientToScreen(position)
        self.SetPosition(position)


    def OnTimer(self, event):
        self.timerCount += self.timerInc
        if self.timerCount >= self.timeout:
	    self.Show(False)
	    return

	self.transLevel -= self.transInc
	if self.transLevel >= 0 and self.CanSetTransparent():
	    self.SetTransparent(self.transLevel)


    def OnEnterWindowStaticText(self, event):
	event.Skip()
        self.EnteredWindow()


    def OnEnterWindow(self, event):
	event.Skip()
        self.EnteredWindow()


    def EnteredWindow(self):
	self.entered += 1

	if self.entered == 1:
	    self.timer.Stop()
	    if self.CanSetTransparent():
		self.SetTransparent(self.fullOpaque)


    def OnLeaveWindowStaticText(self, event):
        event.Skip()
        self.LeftWindow()


    def OnLeaveWindow(self, event):
	event.Skip()
        self.LeftWindow()


    def LeftWindow(self):
	# Ideally this shouldn't happen
	if not self.entered:
	    return

	self.entered -= 1

	if not self.entered:
	    self.timer.Start(self.timerInc * 1000)
	    if self.CanSetTransparent():
		self.SetTransparent(self.transLevel)

	
    def AddMessage(self, message):
        size = self.m_staticTextMessages.GetSize()
        # self.m_staticTextMessages.Wrap(size.width - 10)
        self.m_staticTextMessages.Wrap(350)

	if self.IsShown():
            message += '\n%s' % self.m_staticTextMessages.GetLabel()

        self.m_staticTextMessages.SetLabel(message)
        self.m_staticTextMessages.Wrap(350)
        self.m_staticTextMessages.Refresh()
	self.Show(True)


    def OnSetFocus(self, event):
        win = wx.GetApp().GetTopWindow()
        # if not win.IsActive():
        win.SetFocus()


    def OnLeftUpStaticText(self, event):
        self.Show(False)
